package view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import models.ModelLocator;
	
	import utils.DrawUtils;
	import utils.PNGEncoder;

	public class EditFrame extends BaseFrame
	{
		private static var instance:EditFrame; 
		private var data:Object;
		private var loader:Loader;
		private var txt:TextField;
		private var maxH:Number = 356;
		private var maxW:Number = 308;
		
		private var bitmap:Bitmap;
		private var shape:Shape;
		public var bitmapMask:Shape;
		private var imgBitmap:Bitmap;
		private var bgBitmap:Bitmap;
		private var bgBitmapdata:BitmapData;
		private var console:Console;
		private var noframeW:Number;
		private var noframeH:Number;
		
		/*[Embed(source="assets/navigation/rest.png")]
		private var restCls:Class;*/
		
		public static function getInstance():EditFrame  
		{  
			if(instance==null)  instance = new EditFrame();  
			return instance; 
		}
		public function EditFrame()
		{
			if(instance!=null) throw new Error("Error: Singletons can only be instantiated via getInstance() method!");  
			EditFrame.instance = this;  
			init();
		}
		private function init():void{
			setInfo("<font color='#6d5939' size='16' face='黑体' ><b>调整构图</b></font>");
			loader = new Loader();
			
			txt = new TextField();
			txt.x = 40;
			txt.y = 40;
			txt.selectable = false;
			txt.width = 200;
			txt.height = 20;
			txt.textColor = 0x6c5939;
			
			draw();
			
			/*var restBtn:ImageBtn = new ImageBtn("还原");
			this.addChild(restBtn);*/
			//var restBitmap:Bitmap = new restCls();
			var restBtn:ImageBtn = new ImageBtn("还原");
			//restBtn.addChild(restBitmap);
			this.addChild(restBtn);
			restBtn.buttonMode = true;
			restBtn.x = 654;
			restBtn.y = 8;
			restBtn.addEventListener(MouseEvent.CLICK,restClick);
		}
		private function restClick(event:MouseEvent):void{
			EditFrame.getInstance().console.setRest();
		}
		private function draw():void{
			bgBitmapdata = new BitmapData(this.width,this.height,true,0);
			bgBitmap = new Bitmap(bgBitmapdata);
			this.addChild(bgBitmap);
			
			console = new Console();
			this.addChild(console);
			/*console.x = 763/2;
			console.y = 389/2;*/
			
		}
		public function show(data:Object):void{
			if(shape && shape.parent){
				this.removeChild(shape);
			}
			if(data == null){
				shape = new Shape();
				drawRec(shape.graphics);
				this.addChild(shape);
				return;
			}
			//return;
			this.data = data;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
			loader.load(new URLRequest(data.editPic))
			this.addChild(txt);
		}
		private function drawRec(g:Graphics):void{
			g.clear();
			var wh:Array = ModelLocator.getInstance().size.split("X");
			var wx:int = wh[0];
			var hx:int = wh[1];
			if(wx >= hx){
				noframeW = maxW;
				noframeH = maxW*hx/wx;
			}else{
				noframeH = maxH;
				noframeW = maxH*wx/hx;
			}
			var xpos:Number = (763-noframeW)/2;
			var ypos:Number = (389-noframeH)/2;
			
			var p1:Point = new Point(xpos,ypos);//763,389
			var p2:Point = new Point(xpos+noframeW,ypos);
			var p3:Point = new Point(xpos+noframeW,ypos+noframeH);
			var p4:Point = new Point(xpos,ypos+noframeH);
			g.lineStyle(2,0xaa503c);
			DrawUtils.drawDashed(g,p1,p2,5,5);
			DrawUtils.drawDashed(g,p2,p3,5,5);
			DrawUtils.drawDashed(g,p3,p4,5,5);
			DrawUtils.drawDashed(g,p4,p1,5,5);
			console.rec = new Rectangle(xpos+5,ypos+5,noframeW-10,noframeH-10);
		}
		private function onProgress(event:ProgressEvent):void{
			txt.text = "图框加载" + int(event.bytesLoaded/event.bytesTotal*100) + "%"
		}
		private function onComplete(event:Event):void{
			if(txt.parent)
				this.removeChild(txt);
			if(this.bitmap && this.bitmap.parent){
				this.removeChild(this.bitmap);
			}
			this.bitmap = event.target.content;
			
			var sw:Number = bitmap.width/maxW;
			var sh:Number = bitmap.height/maxH;
			var rate:Number;
			if(sw < sh){
				rate = maxH/bitmap.height;
			}else{
				rate = maxW/bitmap.width;
			}
			bitmap.scaleX = bitmap.scaleY = rate;
			this.addChild(bitmap);
			bitmap.x = (763 - bitmap.width) * 0.5;
			bitmap.y = (389 - bitmap.height) * 0.5;
			//if(console.rec == null)
			console.rec = new Rectangle(bitmap.x+5,bitmap.y+5,bitmap.width-10,bitmap.height-10);
			
			//stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown)
		}
		public function getByteArray():ByteArray{
			var bd:BitmapData = new BitmapData(console.width,console.height);
			bd.draw(this);
			var xpos:Number;
			var ypos:Number;
			var w:Number;
			var h:Number;
			
			if(bitmap){
				xpos = bitmap.x;
				ypos = bitmap.y;
				w = bitmap.width;
				h = bitmap.height;
			}else{
				xpos = (763-noframeW)/2;
				ypos = (389-noframeH)/2;
				w = noframeW;
				h = noframeH;
			}
			var bds:BitmapData = new BitmapData(w,h);
			var rec:Rectangle = new Rectangle(xpos,ypos,w,h);
			var p:Point = new Point();
			bds.copyPixels(bd,rec,p);
			/*var cutbitmap:Bitmap = new Bitmap(bds)
			this.addChild(cutbitmap);*/
			var byte:ByteArray = PNGEncoder.encode(bds);
			return byte;
		}
		
	}
}